package com.fuyin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.util.IdUtil;
import com.fuyin.commons.response.BaseResponse;
import com.fuyin.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author yuanxilan (yuanxilan@keep.com)
 * @Description
 * @date 2021年12月04日 2:13 PM
 * @since
 */
@Controller
@RequestMapping(value = "/verify/verification")
@Slf4j
@Api("登录验证码控制器")
public class VerificationController {

    @Autowired
    private RedisService redisService;

    @GetMapping(produces = "image/png")
    @ResponseBody
    @ApiOperation(value = "获取验证码图片")
    public void getVerificationPicture(HttpServletRequest request, HttpServletResponse response, @RequestParam("key") String key) {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 60, 4, 4);
        try (OutputStream outputStream = response.getOutputStream()) {
//            request.getSession().setAttribute("CAPTCHA_KEY", captcha.getCode());
//            log.info("getVerificationPicture session:{}", request.getSession().getId());
//            log.info("code: {}", captcha.getCode());
            redisService.set(key, captcha.getCode(), 300);
            response.setContentType("text/html; charset=UTF-8");
            response.setContentType("image/png");
            captcha.write(outputStream);
            outputStream.flush();
            response.flushBuffer();
//            response.setHeader("Pragma", "No-cache");
//            response.setHeader("Cache-Control", "no-cache");
//            response.setDateHeader("Expire", 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getrandomkey")
    @ApiOperation("获取随机 key")
    @ResponseBody
    public BaseResponse<String> getRandomKey() {
        // b17f24ff026d40949c85a24f4f375d42
        String id = IdUtil.simpleUUID();
        return BaseResponse.success(id);
    }

    /**
     @PostMapping
     @ResponseBody
     @ApiOperation(value = "验证验证码")
     public BaseResponse verify(HttpServletRequest request, HttpServletResponse response, @ApiParam("验证码") @RequestParam("code") String value) {
     MyUtils.checkParams("验证码不能为空", value);
     boolean ok = request.getSession().getAttribute("CAPTCHA_KEY").equals(value);
     if (!ok) {
     return BaseResponse.error(455, "验证码错误");
     }
     return BaseResponse.success();
     }
     **/
}
